package xyz.gpgl.erp.logic.service.impl;

import com.alibaba.fastjson.JSONArray;
import common.util.base.CommonUtil;
import common.util.mybatis.plus.logic.LogicBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.gpgl.erp.logic.entity.LogicSharesXpathDetail;
import xyz.gpgl.erp.logic.entity.LogicSharesXpathMaster;
import xyz.gpgl.erp.logic.entity.param.LogicSharesXpathMasterParam;
import xyz.gpgl.erp.logic.mapper.LogicSharesXpathMasterMapper;
import xyz.gpgl.erp.logic.service.ILogicSharesXpathDetailService;
import xyz.gpgl.erp.logic.service.ILogicSharesXpathMasterService;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liubo
 * @since 2020-10-31
 */
@Service
public class LogicSharesXpathMasterServiceImpl extends LogicBaseServiceImpl<LogicSharesXpathMasterMapper, LogicSharesXpathMaster> implements ILogicSharesXpathMasterService {

    @Autowired
    private ILogicSharesXpathDetailService logicSharesXpathDetailService;
    @Override
    public void saveSharesXpath(LogicSharesXpathMasterParam param) {
        LogicSharesXpathMaster master = CommonUtil.convertJsonToObject(param.getMaster(), LogicSharesXpathMaster.class);
        List<LogicSharesXpathDetail> list = JSONArray.parseArray(param.getDetails(), LogicSharesXpathDetail.class);
        this.saveOrUpdate(master);
        if(CommonUtil.isNotEmpty(list)){
            list.stream().forEach(row -> {
                row.setMasterId(master.getId());
                this.logicSharesXpathDetailService.saveOrUpdate(row);
            });
        }
    }
}
